<template>
  <div class="categroy">
    <!-- 头部 -->
    <header class="home-header wrap category-header">
      <i class="iconfont icon-caidan" @click="gohome"
        ><van-icon name="arrow-left"
      /></i>
      <div class="header-search">
        <i class="iconfont icon-search"><van-icon name="search" /></i>
        <span class="search-title">家电返场同价11.11</span>
      </div>
      <span
        ><van-icon name="ellipsis" class="swipder" size="35" color="slategray"
      /></span>
    </header>

    <!-- 信息内容 -->
    <div class="category-wrap" ref="contentwrapper">
      <!-- 侧边栏 -->

      <div class="nav-side-wrapper" ref="sidewrapper">
        <van-pull-refresh
          v-model="isLoading"
          success-text="刷新成功"
          @refresh="onRefresh"
        >
          <ul class="nav-slide">
            <li
              v-for="(item, index) in listall"
              :key="index"
              @click="select(index)"
              :class="{ activer: currentIndex === index }"
            >
              {{ item.name }}
            </li>
          </ul>
        </van-pull-refresh>
      </div>
      <!-- 商品数据 -->
      <div class="content-slide" ref="contentwrapper">
        <van-pull-refresh
          v-model="isLoadings"
          success-text="刷新成功"
          @refresh="onRefresh"
        >
          <div>
            <img :src="imgser.mainImgUrl" class="category-main-img" />
            <div
              class="category-list"
              v-for="(items, index) in imgser.list"
              :key="index"
            >
              <p class="catogory-title">{{ items.title }}</p>
              <div
                class="product-item"
                v-for="(ppp, index) in items.productList"
                :key="index"
              >
                <img :src="ppp.imgUrl" alt="" />
                <p class="product-title">{{ ppp.title }}</p>
              </div>
            </div>
          </div>
        </van-pull-refresh>
      </div>
    </div>
    <tab-bar></tab-bar>
  </div>
</template>

<script>
import tabBar from "@/components/tabBar.vue";
import BScroll from "@better-scroll/core";
import Pullup from "@better-scroll/pull-up";
BScroll.use(Pullup);
import { getProcuctList } from "../../api";
export default {
  components: { tabBar },
  data() {
    return {
      //列表
      listall: [],
      imgser: {},
      currentIndex: 0,
      isLoading: false,
      isLoadings:false,
    };
  },
  created() {
    getProcuctList((res) => {
      this.listall = res.data;
      this.imgser = this.listall[0];
      this.$nextTick(() => {
        new BScroll(this.$refs.sidewrapper, {});
        new BScroll(this.$refs.contentwrapper, {});
      });

      // console.log("imgser", this.imgser);
      // console.log("listall", this.listall);
    });
  },

  mounted() {
    // 将回调延迟到下次 DOM 更新循环之后执行。在修改数据之后立即使用它，然后等待 DOM 更新。
    this.$nextTick(function () {
      this.setWrapperHeight();
      // 兼容手机 浏览器 滚动效果
      let wrapper = document.querySelector(".nav-side-wrapper");
      new BScroll(wrapper, {
        click: true,
      });
      new BScroll(this.$refs.contentwrapper, {
        click: true,
      });
      new BScroll(".category-wrap", {
        pullUpLoad: true,
      });
    });
    // setTimeout(() => {
    //   this.setWrapperHeight();
    // }, 300);
  },
  methods: {
    gohome() {
      this.$router.push("/home");
    },
    // 固定 内容 容器的高度
    setWrapperHeight() {
      // 获取 设备屏幕高度
      // console.log(window.screen.height);
      // 获取 Header 高度
      const headEleH = document.querySelector(".category-header").clientHeight;
      // 计算  内容高度
      const contentH = window.screen.height - headEleH - 50;
      // 固定 左侧列表高度
      this.$refs.sidewrapper.style.height = contentH + "px";
      // 固定右侧商品 内容高度
      this.$refs.contentwrapper.style.height = contentH + "px";
    },
    // 选中 左侧列表选项
    select(index) {
      this.currentIndex = index;
      this.imgser = this.listall[index];
    },
    onRefresh() {
      setTimeout(() => {
        this.isLoading = false;
        this.isLoadings = false;
      }, 1000);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../common/style/mixin.scss";
@import "../../common/style/base.scss";
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  @include fj;
  width: 100%;
  height: 100px;
  line-height: 100px;
  padding: 0 30px;
  @include boxSizing;
  font-size: 24px;
  color: #fff;
  z-index: 10000;
  background-color: #fff;
  border-bottom: #ccc 1px solid;
  .header-search {
    background-color: #f7f7f7;
  }
  .iconfont {
    color: slategray;
  }
  .swipder {
    color: slategray;
    line-height: 100px;
  }
}
.category-wrap {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-top: 100px;
  background: #fff;
  border-top: 0.013333rem solid #dcdcdc;
  overflow: hidden;
}
.nav-side-wrapper {
  width: 30%;
  height: 100%;
  overflow: scroll;
}
.nav-slide {
  width: 100%;
  box-sizing: border-box;
  background: #f8f8f8;
}
.nav-slide li {
  width: 100%;
  height: 1.226667rem;
  text-align: center;
  line-height: 1.226667rem;
  font-size: 0.373333rem;
}
.content-slide {
  width: 70%;
  height: 100%;
  overflow: scroll;
  padding: 0 0.266667rem;
  background: #fff;
  box-sizing: border-box;

  .category-main-img {
    width: 100%;
  }
  .category-list {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
  }
  .catogory-title {
    width: 100%;
    font-size: 0.453333rem;
    font-weight: 500;
    padding: 0.533333rem 0;
  }
  .product-item {
    width: 33.3333%;
    margin-bottom: 0.266667rem;
    text-align: center;
    font-size: 0.4rem;
  }
  .product-item img {
    width: 100%;
  }
}
.activer {
  color: red;
  background-color: white;
}
</style>